using System.Security.Principal;

using LocalBoard.Domain;
using LocalBoard.Controller;

namespace LocalBoard.Web
{
	/// <summary>
	/// Summary description for ClubPrincipal.
	/// </summary>
	public class ClubPrincipal : IPrincipal
	{
		private IIdentity _identity = null;

		public ClubPrincipal(IIdentity identity)
		{
			_identity = identity;
			_identity = new GenericIdentity(GetUserIdWithoutDomain());
		}

		private Profile _profile = null;
		public Profile UserProfile
		{
			get
			{
				if(_profile == null && _identity != null)
					_profile = new AdminController(Global.AccessorFactory).GetUser(_identity.Name);
				return _profile;
//				if(_profile == null)
//				{
//					if(_identity != null && _identity.Name != Security.AnonymousUser.UserName)
//						_profile = new AdminController(Global.AccessorFactory).GetUser(GetUserIdWithoutDomain());
//					if(_profile == null && _allowAnonymousUser) //User who's not logged in or logged in but not registered
//						_profile = Security.AnonymousUser;
//				}
//				return _profile;
			}
		}

		private string GetUserIdWithoutDomain()
		{
			int indexOfBackSlash = _identity.Name.IndexOf("\\");
			return indexOfBackSlash >= 0 ? _identity.Name.Substring(indexOfBackSlash + 1) : _identity.Name;
		}

		#region IPrincipal Members
		public IIdentity Identity
		{
			get {return _identity;}
		}

		public bool IsInRole(string role)
		{
			return UserProfile.IsInRole(role);
		}

		public bool IsInRole(int roleId)
		{
			return UserProfile.IsInRole(roleId);
		}
		#endregion
	}
}